package cn.com.niit.product.controller;

import cn.com.niit.product.entity.Product;
import cn.com.niit.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RefreshScope
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //注入自定义信息
    @Value("${test.info}")
    private String info;

    //注入端口号
    @Value("${server.port}")
    private String port;

    //注入IP
    @Value("${spring.cloud.client.ip-address}")
    private String ip;


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Product findById(@PathVariable Long id) throws InterruptedException {
        Product product = productService.findById(id);
        //添加商品描述，用于区别
        product.setProductDesc("from "+ip+" "+port);
        return product;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String save(@RequestBody Product product) {
        productService.save(product);
        return "保存成功";
    }

    @RequestMapping(value = "/test")
    public String test() {
        return info;
    }
}
